#include <iostream>
#include "3761.h"
#include "filetext.h"
#include "sqlite++.h"

#define DEFINETEST(AP, Fn) \
void AP##Fn##Parser(void)\
{\
	AP##Afn0dParser a;\
\
	for(int i = 1; i <= 3; i++)\
	{\
		a.F##Fn(i) = AP##Afn0df##Fn(i);\
	}\
	Buffer b;\
	a.append(b);\
	a.clear();\
	cout << __func__ << endl;\
	cout << "length=" << b.length << endl;\
	cout << b.toString() << endl;\
}
DEFINETEST(Active, 1)
DEFINETEST(Active, 2)
DEFINETEST(Active, 3)
DEFINETEST(Active, 4)
DEFINETEST(Active, 5)
DEFINETEST(Active, 6)
DEFINETEST(Active, 7)
DEFINETEST(Active, 8)

DEFINETEST(Pasive, 1)
DEFINETEST(Pasive, 2)
DEFINETEST(Pasive, 3)
DEFINETEST(Pasive, 4)
DEFINETEST(Pasive, 5)
DEFINETEST(Pasive, 6)
DEFINETEST(Pasive, 7)
DEFINETEST(Pasive, 8)

void testParser(void)
{
#define TESTHANDLER(AP,Fn) \
		AP##Fn##Parser()

	TESTHANDLER(Active, 1);
	TESTHANDLER(Active, 2);
	TESTHANDLER(Active, 3);
	TESTHANDLER(Active, 4);
	TESTHANDLER(Active, 5);
	TESTHANDLER(Active, 6);
	TESTHANDLER(Active, 7);
	TESTHANDLER(Active, 8);

	TESTHANDLER(Pasive, 1);
	TESTHANDLER(Pasive, 2);
	TESTHANDLER(Pasive, 3);
	TESTHANDLER(Pasive, 4);
	TESTHANDLER(Pasive, 5);
	TESTHANDLER(Pasive, 6);
	TESTHANDLER(Pasive, 7);
	TESTHANDLER(Pasive, 8);
}
void testAfn0dfxx(void)
{
	ActiveAfn0df1 a1;
	ActiveAfn0df2 a2;
	ActiveAfn0df3 a3;
	ActiveAfn0df4 a4;

	PasiveAfn0df1 p1;
	PasiveAfn0df2 p2;
	PasiveAfn0df3 p3;
	PasiveAfn0df4 p4;

	cout << "a1:" << toString(a1) << endl;
	cout << "a2:" << toString(a2) << endl;
	cout << "a3:" << toString(a3) << endl;
	cout << "a4:" << toString(a4) << endl;
	cout << "p1:" << toString(p1) << endl;
	cout << "p2:" << toString(p2) << endl;
	cout << "p3:" << toString(p3) << endl;
	cout << "p4:" << toString(p4) << endl;
	for(int i = 1; i < 03; i++)
	{
		p1.rate(i);
		p1._fwttap  = 111.222;
		p1._fwttrp  = 33.44;
		p1._ps1ttrp = 555.666;
		p1._ps4ttrp = 777.888;
		for(int x = 0; x < p1._rate+1; x++)
		{
			int n = x + 1;
			p1._fwap[x]  = 123.455 * n;
			p1._fwrp[x]  = 78.915 * n;
			p1._ps1rp[x] = 321.432 * n;
			p1._ps4rp[x] = 888.432 * n;
		}
		cout << "[" << i << "](" << p1.length() << ") " << hexBySize(p1) << endl;
	}
}
void check(void)
{
	uint8_t data[] = {
		0x11, 0x22, 0x68, 0x33, 0x44, 0x68, 0x16,
		0x68, 0xCA, 0x00, 0xCA, 0x00, 0x68, 0x4A, 
		0x01, 0x43, 0x00, 0x01, 0x02, 0x04, 0x71, 
		0x01, 0x01, 0x01, 0x08, 0x0F, 0x01, 0x34, 
		0x23, 0x29, 0x27, 0x19, 0x01, 0x00, 0x02, 
		0x01, 0x01, 0x08, 0x0F, 0x01, 0x34, 0x23, 
		0x29, 0x27, 0x19, 0x01, 0x00, 0x00, 0x00, 
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
		0xBF, 0x16, 0xAA, 0x55, 0x16, 0x68, 0x68};
	Buffer buffer;

	for(int i = 0; i < sizeof(data); i++)
	{
		buffer.push(data[i]);
		if(buffer.check())
		{
			cout << buffer.frame.toString() << endl;
			break;
		}
	}

}
void testSqlite(int n=100)
{
	Sqlite db("/dev/shm/test.db");

	FileText create("sql/afn0df01_pasive.create");
	FileText insert("sql/afn0df01_pasive.insert");
	FileText select("sql/afn0df01_pasive.select");
	FileText deleti("sql/afn0df01_pasive.delete");

	cout << (db << deleti.text).errmsg() << endl;
	cout << (db << create.text).errmsg() << endl;
	for(int i = 0; i < n; i++)
		cout << (db << insert.text).errmsg() << endl;
	cout << (db << select.text).errmsg() << endl;
	cout << db.result.str() << endl;
	cout << db.result.size() << endl;
	if(db.result.size() > 0)
	{
		PasiveAfn0df1 f1;
		f1.set( db.result.front() );
		cout << hexByLength(f1) << endl;
	}
}
int main(int argc, char **argv)
{
	testSqlite(argc < 2 ? 1 : atoi(argv[1]));return 0;
	FrameMaker fmaker;

	testParser();

	fmaker.makeLogin();
	cout << fmaker.sendbuf.frame.toString() << endl;
	fmaker.makeHeartbeat();
	cout << fmaker.sendbuf.frame.toString() << endl;

	return 0;
}
